/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider.megaproxylist;

import cn.hutool.core.util.ReUtil;
import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.core.tester.ProxyTypeEnum;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;
import org.shoulder.core.converter.LocalDateTimeConverter;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * https://www.megaproxylist.net/【只有一页】 每几小时钟代理
 *
 * @author lym
 */
//@Component
public class MegaProxyListResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    // Transparent 透明 Anonymous 匿名 Distorting 混淆 Elite
    private static final Map<String, String> anonymityMap = Map.of(
            "Transparent", "透明",
            "Anonymous", "匿名",
            "Distorting", "混淆",
            "Elite", "高匿"
    );

    public MegaProxyListResultHandler() {
        super("megaproxylist");
    }

    protected Duration defaultLimitRateDuration() {
        return Duration.ofSeconds(2);
    }

    @Override
    public void startSpiders() {
        // 不限地区
        IpProxySpiderTask task = createTask("https://www.megaproxylist.net/");
        SpiderTaskQueue.putTask(task);
    }

    /**
     * 每 30 分钟触发一次
     */
    @Override
    public String executeCron() {
        return "0 0 */2 * * ?";
    }

    /**
     * <tr>
     * <td style="text-align:left;">ec2-54-153-87-94.us-west-1.compute.amazonaws.com:80</td>
     * <td style="text-align:center; color:green;"><strong>Elite</strong></td>
     * <td style="text-align:left;">US</td><td style="text-align:right;">134&nbsp;</td>
     * <td style="text-align:center;">21635 / 51</td>
     * <td style="text-align:center;">100%</td>
     * <td style="text-align:center;">2022-04-07 01:11:44</td>
     * </tr>
     */
    @Override
    public void doHandle(IpProxySpiderTask task, String html) {

        String regex = "<tr><td style=\"text-align:left;\">(.+?):(\\d+?)</td>" +
                "<td style=\".*?>(\\w+?)</.*?td><td style=\"text-align:left;\">(.+?)</td>" +
                "<td style=\"text-align:right;\">(\\d+?)&nbsp;</td>" +
                "<td style=\"text-align:center;\">(\\d+?) / (\\d+?)</td>" +
                "<td style=\"text-align:center;\">(.*?)%</td>" +
                "<td style=\"text-align:center;\">(.*?)</td></tr>";

        List<String> ipList = ReUtil.findAll(regex, html, 1);
        List<String> portList = ReUtil.findAll(regex, html, 2);
        List<String> anonymityList = ReUtil.findAll(regex, html, 3);
        List<String> locationList = ReUtil.findAll(regex, html, 4);
        List<String> responseTimeList = ReUtil.findAll(regex, html, 5);
        List<String> successCountList = ReUtil.findAll(regex, html, 6);
        List<String> failCountList = ReUtil.findAll(regex, html, 7);
        List<String> successPercentList = ReUtil.findAll(regex, html, 8);
        List<String> lastVerificationTimeList = ReUtil.findAll(regex, html, 9);

        for (int i = 1; i < ipList.size(); i++) {
            String anonymity = anonymityList.get(i);
            String anonymityValue = anonymityMap.get(anonymity);
            ProxyInfo proxyInfo = ProxyInfo.builder()
                    .source(taskName)
                    .ip(ipList.get(i))
                    .port(Integer.parseInt(portList.get(i)))
                    .proxyTypeEnum(ProxyTypeEnum.UNKNOWN)
                    .type(ProxyTypeEnum.UNKNOWN.getType())
                    .anonymity(anonymityValue)
                    .location(locationList.get(i))
                    .responseTime(Long.parseLong(responseTimeList.get(i)))
                    .successCount(Long.parseLong(successCountList.get(i)))
                    .failCount(Long.parseLong(failCountList.get(i)))
                    .successPercentage(Integer.parseInt(successPercentList.get(i)))
                    .lastVerificationTime(
                            LocalDateTimeConverter.INSTANCE.convert(lastVerificationTimeList.get(i))
                    )
                    .build();
            // 保存到文件
            addProxyToSourceRepository(proxyInfo);
        }
    }

}